<?php
class Company extends Zend_Db_Table {
	protected $_name = "cc_company";
	protected $_primamy = "id";
	private $db;
	private $compArr;//所有公司信息数组
	private $compCnt;//总数
	
	/**
	 * 
	 * @access public
	 * @return 
	 */
	public function __construct(){
		$this->db = Zend_Db_Table::getDefaultAdapter();
		parent::_setup();
		$this->getCompany();
	}
	/**
	 * 增加点击数
	 * @param string $id
	 * @param int $clickcnt
	 */
	public function addClick($id, $clickcnt) {
		$data = array ('clickcnt' => $clickcnt );
		$updcnt = $this->update ( $data, "id=" . $id );
		return $updcnt;
	}
	/**
	 * 取所有公司信息和总数，放到公司数组和总数数组
	 * @access private
	 * @return 
	 */
	private function getCompany(){
		$where = $this->db->quoteInto('state=?',1);
		$rowset = $this->fetchAll($where);
		$this->compArr = $rowset->toArray();
		$this->compCnt = $rowset->count();
	}
	/**
	 * 取公司总数
	 * @access public
	 * @return int 公司总数
	 */
	public function getCompanyCnt(){
		return $this->compCnt;
	}
	/**
	 * 取所有公司
	 * @access public
	 * @return array 所有公司
	 */
	public function getAllCompany(){
		return $this->compArr;
	}
	/**
	 * 据公司ID取公司信息
	 * @access public
	 * @return array 单行数组
	 */
	public function getCompanyById($companyId){
		$where = $this->db->quoteInto("id=?",$companyId);
		$rowset = $this->fetchRow($where);
		return $rowset->toArray();
	}
	/**
	 * 修改公司信息
	 * @access public
	 * @return 
	 */
	public function updCompany($data,$id){
		$where = $this->db->quoteInto('id=?',$id);
		$updCnt = $this->update($data,$where);
		return $updCnt;
	}
	/**
	 * 添加公司
	 * @access public
	 * @return int 新增ID
	 */
	public function insCompany($data){
		$insid = $this->insert($data);
		return $insid;
	}
	
	/**
	 * 据所给条件查询公司
	 * @access public
	 */
	public function getCompanyByCond($type){
		if ($type == "all") 
			$where = $this->db->quoteInto("btype like ?","%");
		else
			$where = $this->db->quoteInto("btype like ?",$type);
		$rowset = $this->fetchAll($where);
		$res = $rowset->toArray();
		return $rowset->count();
	}

}